package com.aooci.ordercat.service.impl;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aooci.ordercat.dao.DiningTableDAO;
import com.aooci.ordercat.dao.ItemCategoryDAO;
import com.aooci.ordercat.dao.ItemDAO;
import com.aooci.ordercat.dao.OrdercatDAO;
import com.aooci.ordercat.dao.OrderedItemDAO;
import com.aooci.ordercat.dao.RecetteDAO;
import com.aooci.ordercat.dao.SortGroupDAO;
import com.aooci.ordercat.dao.TakeawayDAO;
import com.aooci.ordercat.domain.DiningTable;
import com.aooci.ordercat.domain.Item;
import com.aooci.ordercat.domain.ItemCategory;
import com.aooci.ordercat.domain.Ordercat;
import com.aooci.ordercat.domain.OrderedItem;
import com.aooci.ordercat.domain.Recette;
import com.aooci.ordercat.domain.SortGroup;
import com.aooci.ordercat.domain.Takeaway;
import com.aooci.ordercat.enums.DiningTableStatus;
import com.aooci.ordercat.enums.OrderStatus;
import com.aooci.ordercat.enums.OrderType;
import com.aooci.ordercat.enums.OrderedItemType;
import com.aooci.ordercat.enums.TakeawayStatus;
import com.aooci.ordercat.service.OrderOperationService;
import com.aooci.ordercat.vo.CheckoutVo;
import com.aooci.ordercat.vo.ItemVo;
import com.aooci.ordercat.vo.OperatedOrderVo;
import com.aooci.ordercat.vo.OrderedItemVo;

@Service("orderOperationService")  
@Transactional
public class OrderOperationServiceImpl implements OrderOperationService, Serializable {
	private static final long serialVersionUID = -5978004761103388486L;

	
	private DiningTableDAO diningTableDAO;
	public void setDiningTableDAO(DiningTableDAO diningTableDAO) {
		this.diningTableDAO = diningTableDAO;
	}
	
	private RecetteDAO recetteDAO;
	/**
	 * @param recetteDAO the recetteDAO to set
	 */
	public void setRecetteDAO(RecetteDAO recetteDAO) {
		this.recetteDAO = recetteDAO;
	}

	private TakeawayDAO takeawayDAO;
	/**
	 * @param takeawayDAO the takeawayDAO to set
	 */
	public void setTakeawayDAO(TakeawayDAO takeawayDAO) {
		this.takeawayDAO = takeawayDAO;
	}

	private OrdercatDAO ordercatDAO;
	public void setOrdercatDAO(OrdercatDAO ordercatDAO) {
		this.ordercatDAO = ordercatDAO;
	}

	private ItemDAO itemDAO;
	public void setItemDAO(ItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	
	private ItemCategoryDAO itemCategoryDAO;
	public void setItemCategoryDAO(ItemCategoryDAO itemCategoryDAO) {
		this.itemCategoryDAO = itemCategoryDAO;
	}
	
	private OrderedItemDAO orderedItemDAO;
	public void setOrderedItemDAO(OrderedItemDAO orderedItemDAO) {
		this.orderedItemDAO = orderedItemDAO;
	}

	private SortGroupDAO sortGroupDAO;
	/**
	 * @param sortGroupDAO the sortGroupDAO to set
	 */
	public void setSortGroupDAO(SortGroupDAO sortGroupDAO) {
		this.sortGroupDAO = sortGroupDAO;
	}

	@Override
	public List<ItemCategory> loadItemCategories() {
		return this.itemCategoryDAO.loadAllByOrder();
	}

	@Override
	public List<ItemVo> loadItemsByItemCategoryId(int itemCategoryId) {
		return this.itemDAO.selectByItemCategoryId(itemCategoryId);
	}

	@Override
	public List<OrderedItemVo> loadOrderedItems(int ordercatId) {
		List<OrderedItemVo> orderedItems = new ArrayList<OrderedItemVo>();
		boolean switcher = false;
		for(int sortGroup:this.orderedItemDAO.selectSortGroupByOrdercatId(ordercatId)){
			for(OrderedItemVo orderedItemVo: this.orderedItemDAO.selectByOrdercatIdAndSortGroup(ordercatId, sortGroup)){
				if(switcher){
					orderedItemVo.setFirst(true);
					switcher = false;
				}
				orderedItems.add(orderedItemVo);
			}
			switcher = true;
		}
		return orderedItems;
		//return this.orderedItemDAO.selectByOrdercatId(ordercatId);
	}

	@Override
	public void orderItem(OperatedOrderVo operatedOrderVo, int itemId) {
		Item item = this.itemDAO.findById(itemId);
		
		
		Ordercat ordercat = this.ordercatDAO.findById(operatedOrderVo.getOrdercat().getId());
		OrderedItem orderedItem = this.orderedItemDAO.findOrderedItem(operatedOrderVo.getOrdercat().getId(), itemId);
		
		if(orderedItem != null){
			orderedItem.setCount(orderedItem.getCount() + 1);
			orderedItem.setModifiedCount(orderedItem.getModifiedCount() + 1);
		}else{
			orderedItem = new OrderedItem();
			orderedItem.setOrdercat(ordercat);
			orderedItem.setItem(item);
			
			orderedItem.setCount(1);
			orderedItem.setModifiedCount(1);
			orderedItem.setSortGroup(item.getItemCategory().getSortGroup());
			orderedItem.setType(OrderedItemType.NORMAL);
			orderedItem.setUnitPrice(item.getUnitPrice());
		}

		this.orderedItemDAO.save(orderedItem);
		ordercat.setTotal(this.orderedItemDAO.getOrderTotal(operatedOrderVo.getOrdercat().getId()));
		
		this.ordercatDAO.save(ordercat);
		operatedOrderVo.setOrdercat(ordercat);
		
		operatedOrderVo.setHasAddingKitchenItems(this.orderedItemDAO.hasAddingKitchenItems(ordercat.getId()));
		operatedOrderVo.setHasKitchenItems(this.orderedItemDAO.hasKitchenItems(ordercat.getId()));
		
	}

	@Override
	public OperatedOrderVo createDiningTableOrder(int diningTableId, int nofClient) {
		Ordercat ordercat = new Ordercat();
		ordercat.setType(OrderType.DINING_HALL);
		ordercat.setStatus(OrderStatus.SERVICING);
		this.ordercatDAO.save(ordercat);
		
		DiningTable diningTable = this.diningTableDAO.findById(diningTableId);
		diningTable.setOrdercat(ordercat);
		diningTable.setNofClient(nofClient);
		diningTable.setStatus(DiningTableStatus.OCCUPIED);
		this.diningTableDAO.save(diningTable);
		this.createNewRecetteEntity(ordercat, 0, diningTable);
		return new OperatedOrderVo(ordercat, diningTable, new HashMap<Integer, Integer>(), false, false);
	}
	
	private void createNewRecetteEntity(Ordercat ordercat, int discountRate, DiningTable diningTable){
		Recette recette = new Recette();
		recette.setOrdercat(ordercat);
		recette.setOrderCreation(ordercat.getCreationTimestamp());
		recette.setDiscountRate(discountRate);
		recette.setDiscount(0);
		recette.setFinalTotal(0);
		recette.setTva21(0);
		recette.setTva12(0);
		recette.setOffert(0);
		recette.setTva6(0);
		
		if(OrderType.DINING_HALL.equals(ordercat.getType())){
			recette.setNumberOfClient(diningTable.getNofClient());
			recette.setOrderLabel(diningTable.getLabel());
		}else{
			recette.setNumberOfClient(0);
			recette.setOrderLabel("TA");
		}
		this.recetteDAO.save(recette);
	}
	
	@Override
	public OperatedOrderVo loadDiningTableOrder(int diningTableId) {
		DiningTable diningTable = this.diningTableDAO.findById(diningTableId);
		Ordercat ordercat = this.ordercatDAO.findById(diningTable.getOrdercat().getId());		
		Map<Integer, Integer> menuItemDetail = new HashMap<Integer, Integer>();
		for(OrderedItemVo orderedItemVo:this.orderedItemDAO.selectByOrdercatId(ordercat.getId())){
			if(orderedItemVo.getItem().getItemCategory().isCombo()){
				menuItemDetail.put(orderedItemVo.getItem().getId(), orderedItemVo.getCount());
			}
		}
		return new OperatedOrderVo(ordercat, diningTable, menuItemDetail, this.orderedItemDAO.hasKitchenItems(ordercat.getId()), this.orderedItemDAO.hasAddingKitchenItems(ordercat.getId()));
	}
	
	@Override
	public OperatedOrderVo loadTakeawayOrder(int takeawayId) {
		Takeaway takeaway = this.takeawayDAO.findById(takeawayId);
		Ordercat ordercat = this.ordercatDAO.findById(takeaway.getOrdercat().getId());
		Map<Integer, Integer> menuItemDetail = new HashMap<Integer, Integer>();
		for(OrderedItemVo orderedItemVo:this.orderedItemDAO.selectByOrdercatId(ordercat.getId())){
			if(orderedItemVo.getItem().getItemCategory().isCombo()){
				menuItemDetail.put(orderedItemVo.getItem().getId(), orderedItemVo.getCount());
			}
		}
		return new OperatedOrderVo(ordercat, takeaway, menuItemDetail, this.orderedItemDAO.hasKitchenItems(ordercat.getId()), this.orderedItemDAO.hasAddingKitchenItems(ordercat.getId()));
	}

	@Override
	public void discardDiningTableOrder(int diningTableId) {
		DiningTable diningTable = this.diningTableDAO.findById(diningTableId);
		if(diningTable != null){
			Ordercat ordercat = this.ordercatDAO.findById(diningTable.getOrdercat().getId());
			
			ordercat.setStatus(OrderStatus.DISCARDED);
			this.ordercatDAO.save(ordercat);
		
			diningTable.setNofClient(0);
			diningTable.setStatus(DiningTableStatus.FREE);
			diningTable.setOrdercat(null);
			this.diningTableDAO.save(diningTable);
		}
	}
	
	@Override
	public void discardTakeawayOrder(int takeawayId) {
		Takeaway takeaway = this.takeawayDAO.findById(takeawayId);
		if(takeaway != null){
			Ordercat ordercat = this.ordercatDAO.findById(takeaway.getOrdercat().getId());
			ordercat.setStatus(OrderStatus.DISCARDED);
			this.ordercatDAO.save(ordercat);
			
			takeaway.setRemark("");
			takeaway.setStatus(TakeawayStatus.FREE);
			takeaway.setTakingTimestamp(null);
			takeaway.setOrdercat(null);
			this.takeawayDAO.save(takeaway);
		}
		
	}

	@Override
	public void disorderItem(OperatedOrderVo operatedOrderVo, int orderedItemEntityId) {
		OrderedItem orderedItem = this.orderedItemDAO.findById(orderedItemEntityId);
		Ordercat ordercat = this.ordercatDAO.findById(orderedItem.getOrdercat().getId());
		
		this.orderedItemDAO.delete(orderedItem);
		ordercat.setTotal(this.orderedItemDAO.getOrderTotal(ordercat.getId()));
		this.ordercatDAO.save(ordercat);
		operatedOrderVo.setOrdercat(ordercat);
		operatedOrderVo.setHasAddingKitchenItems(this.orderedItemDAO.hasAddingKitchenItems(ordercat.getId()));
		operatedOrderVo.setHasKitchenItems(this.orderedItemDAO.hasKitchenItems(ordercat.getId()));
	}

	@Override
	public void updateOrderedItem(OperatedOrderVo operatedOrderVo, OrderedItemVo orderedItemVo) {
		OrderedItem orderedItem = this.orderedItemDAO.findById(orderedItemVo.getEntityId());
		orderedItem.setModifiedCount(orderedItemVo.getCount() + orderedItemVo.getModifiedCount() - orderedItem.getCount());
		//orderedItem.setModifiedCount(orderedItemVo.getModifiedCount());
		orderedItem.setCount(orderedItemVo.getCount());
		orderedItem.setUnitPrice(orderedItemVo.getUnitPrice());
		orderedItem.setRemark(orderedItemVo.getRemark());
		orderedItem.setType(orderedItemVo.getType());
		orderedItem.setSortGroup(orderedItemVo.getSortGroup());

		this.orderedItemDAO.save(orderedItem);
		
		Ordercat ordercat = this.ordercatDAO.findById(orderedItem.getOrdercat().getId());
		ordercat.setTotal(this.orderedItemDAO.getOrderTotal(ordercat.getId()));
		this.ordercatDAO.save(ordercat);
		operatedOrderVo.setOrdercat(ordercat);
		operatedOrderVo.setHasAddingKitchenItems(this.orderedItemDAO.hasAddingKitchenItems(ordercat.getId()));
		operatedOrderVo.setHasKitchenItems(this.orderedItemDAO.hasKitchenItems(ordercat.getId()));
	}

	@Override
	public List<DiningTable> loadFreeDiningTablesByCategoryId(int diningTableCategoryId) {
		return this.diningTableDAO.findTableByCategoryIdAndStatus(diningTableCategoryId, DiningTableStatus.FREE);
	}

	@Override
	public DiningTable changeTable(DiningTable currentTable, int targetTableId) {
		DiningTable targetTable = this.diningTableDAO.findById(targetTableId);
		targetTable.setNofClient(currentTable.getNofClient());
		targetTable.setOrdercat(currentTable.getOrdercat());
		targetTable.setStatus(currentTable.getStatus());
		this.diningTableDAO.save(targetTable);
		
		currentTable = this.diningTableDAO.findById(currentTable.getId());
		currentTable.setNofClient(0);
		currentTable.setOrdercat(null);
		currentTable.setStatus(DiningTableStatus.FREE);
		this.diningTableDAO.save(currentTable);
		
		return targetTable;
	}

	@Override
	public void checkout(OperatedOrderVo operatedOrderVo) {
		DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
		otherSymbols.setDecimalSeparator('.');
		otherSymbols.setGroupingSeparator(','); 
		DecimalFormat df =new DecimalFormat("#.00", otherSymbols);
		
		int orderId = operatedOrderVo.getOrdercat().getId();
		Recette recette = this.recetteDAO.findByOrdercatId(orderId);

		if(OrderType.DINING_HALL.equals(operatedOrderVo.getOrdercat().getType())){
			for(OrderedItemVo orderedItemVo : this.orderedItemDAO.selectByOrdercatId(operatedOrderVo.getOrdercat().getId())){
				if(OrderedItemType.NORMAL.equals(orderedItemVo.getType())){
					if(Float.valueOf("0.21").equals(orderedItemVo.getItem().getItemCategory().getVat())){
						recette.setTva21(recette.getTva21() + (orderedItemVo.getCount() * orderedItemVo.getUnitPrice()));
					}else if(Float.valueOf("0.12").equals(orderedItemVo.getItem().getItemCategory().getVat())){
						recette.setTva12(recette.getTva12() + (orderedItemVo.getCount() * orderedItemVo.getUnitPrice()));
					}
				}else if(OrderedItemType.HOSTED.equals(orderedItemVo.getType())){
					recette.setOffert(recette.getOffert() + (orderedItemVo.getCount() * orderedItemVo.getUnitPrice()));
				}
			}
			if(recette.getDiscountRate() > 0){
				//recette.setDiscount(Float.valueOf(df.format(recette.getFinalTotal() * ((float)recette.getDiscountRate()/100))));
				recette.setTva21(Float.valueOf(df.format(recette.getTva21() * (1 - (float)recette.getDiscountRate()/100))));
				recette.setTva12(Float.valueOf(df.format(recette.getTva12() * (1 - (float)recette.getDiscountRate()/100))));
			}
		}else{
			recette.setTva6(recette.getFinalTotal());
		}
		
		if(recette.getDiscountRate() > 0){
			recette.setDiscount(Float.valueOf(df.format(operatedOrderVo.getOrdercat().getTotal() * ((float)recette.getDiscountRate()/100))));
		}
		this.recetteDAO.save(recette);
		
		Ordercat ordercat = this.ordercatDAO.findById(operatedOrderVo.getOrdercat().getId());
		ordercat.setStatus(OrderStatus.PAID);
		this.ordercatDAO.save(ordercat);
		
		if(operatedOrderVo.getDiningTable() != null){
			DiningTable diningTable = this.diningTableDAO.findById(operatedOrderVo.getDiningTable().getId());
			diningTable.setStatus(DiningTableStatus.PAID);
			this.diningTableDAO.save(diningTable);
		}else if(operatedOrderVo.getTakeaway() != null) {
			Takeaway takeaway = this.takeawayDAO.findById(operatedOrderVo.getTakeaway().getId());
			takeaway.setStatus(TakeawayStatus.PAID);
			this.takeawayDAO.save(takeaway);
		}
		operatedOrderVo.setOrdercat(ordercat);
	}


	@Override
	public void clearOrder(OperatedOrderVo operatedOrderVo, CheckoutVo checkoutVo) {

		Recette recette = this.recetteDAO.findByOrdercatId(operatedOrderVo.getOrdercat().getId());
		//CheckoutVo checkoutVo = this.recetteDAO.findCheckouVoByOrdercatId(operatedOrderVo.getOrdercat().getId());
		recette.setCard(checkoutVo.getCard());
		recette.setCash(checkoutVo.getCash());
		recette.setCheque(checkoutVo.getCheque());
		//recette.setOffert(checkoutVo.getOffert());
		//recette.setTip(checkoutVo.getTip());

		this.recetteDAO.save(recette);
		
		Ordercat ordercat = this.ordercatDAO.findById(operatedOrderVo.getOrdercat().getId());
		ordercat.setStatus(OrderStatus.PAID);
		this.ordercatDAO.save(ordercat);
		
		if(operatedOrderVo.getDiningTable() != null){
			DiningTable diningTable = this.diningTableDAO.findById(operatedOrderVo.getDiningTable().getId());
			diningTable.setStatus(DiningTableStatus.FREE);
			diningTable.setNofClient(0);
			diningTable.setOrdercat(null);
			this.diningTableDAO.save(diningTable);
		}else if(operatedOrderVo.getTakeaway() != null) {
			Takeaway takeaway = this.takeawayDAO.findById(operatedOrderVo.getTakeaway().getId());
			takeaway.setStatus(TakeawayStatus.FREE);
			takeaway.setTakingTimestamp(null);
			takeaway.setRemark("");
			takeaway.setOrdercat(null);
			this.takeawayDAO.save(takeaway);
		}
	}

	@Override
	public Takeaway changeTakingTime(Takeaway currentTakeaway,
			Date newTakingTime) {
		Takeaway takeaway = this.takeawayDAO.findById(currentTakeaway.getId());
		Calendar myCal = Calendar.getInstance();
		myCal.setTime(newTakingTime);
		takeaway.setTakingTimestamp(myCal);
		this.takeawayDAO.save(takeaway);
		return takeaway;
	}
	
	@Override
	public List<SortGroup> loadKitchenPrintableSortGroups() {
		return this.sortGroupDAO.selectSortGroupBySelectable(true);
	}

	static final long ONE_MINUTE_IN_MILLIS=60000;	// millisecs
	static final int DEFAULT_ADDING_TAKING_MINUTES = 30;
	@Override
	public OperatedOrderVo createTakeawayOrder() {
		Takeaway newTakeaway = this.takeawayDAO.findTakeawaysByStatus(TakeawayStatus.FREE).get(0);
		Date takingTimestamp = new Date(new Date().getTime() + (DEFAULT_ADDING_TAKING_MINUTES * ONE_MINUTE_IN_MILLIS));
		Ordercat ordercat = new Ordercat();
		ordercat.setType(OrderType.TAKEAWAY);
		ordercat.setStatus(OrderStatus.SERVICING);
		this.ordercatDAO.save(ordercat);
		
		Takeaway takeaway = this.takeawayDAO.findById(newTakeaway.getId());
		takeaway.setOrdercat(ordercat);
		
//		long takingTime=takingTimestamp.getTime();
//		Date afterAddingTenMins=new Date(takingTime + (DEFAULT_ADDING_TAKING_MINUTES * ONE_MINUTE_IN_MILLIS));
		Calendar myCal = Calendar.getInstance();
		myCal.setTime(takingTimestamp);
		takeaway.setTakingTimestamp(myCal);
		takeaway.setStatus(TakeawayStatus.OCCUPIED);
		this.takeawayDAO.save(takeaway);
		this.createNewRecetteEntity(ordercat, 10, null);
		return new OperatedOrderVo(ordercat, takeaway, new HashMap<Integer, Integer>(), false, false);
	}

//	@Override
//	public CheckoutVo createCheckoutVo(OperatedOrderVo operatedOrderVo) {
//		DecimalFormat df =new DecimalFormat("#.00");  
//		CheckoutVo checkoutVo = new CheckoutVo();
//		if(OrderType.TAKEAWAY.equals(operatedOrderVo.getOrdercat().getType())){
//			checkoutVo.setTotalToPay(Float.valueOf(df.format(operatedOrderVo.getOrdercat().getTotal() * (1-SystemParameter.DISCOUNT_FOR_TAKEAWAY))));
//			checkoutVo.setDiscountRate((int) (SystemParameter.DISCOUNT_FOR_TAKEAWAY * 100));
//			checkoutVo.setDiscount(Float.valueOf(df.format(operatedOrderVo.getOrdercat().getTotal() * SystemParameter.DISCOUNT_FOR_TAKEAWAY)));
//		}else{
//			checkoutVo.setTotalToPay(operatedOrderVo.getOrdercat().getTotal());
//		}
//		checkoutVo.setCash(checkoutVo.getTotalToPay());
//		return checkoutVo;
//	}

	@Override
	public void updateCheckoutVo(CheckoutVo checkoutVo, OperatedOrderVo operatedOrderVo) {
		DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
		otherSymbols.setDecimalSeparator('.');
		otherSymbols.setGroupingSeparator(','); 
		DecimalFormat df =new DecimalFormat("#.00", otherSymbols);
		
		Recette recette = this.recetteDAO.findByOrdercatId(operatedOrderVo.getOrdercat().getId());
		recette.setDiscountRate(checkoutVo.getDiscountRate());

//		if(OrderType.DINING_HALL.equals(operatedOrderVo.getOrdercat().getType())){
//			for(OrderedItemVo orderedItemVo : this.orderedItemDAO.selectByOrdercatId(operatedOrderVo.getOrdercat().getId())){
//				if(OrderedItemType.NORMAL.equals(orderedItemVo.getType())){
//					if(Float.valueOf("0.21").equals(orderedItemVo.getItem().getItemCategory().getVat())){
//						recette.setTva21(recette.getTva21() + (orderedItemVo.getCount() * orderedItemVo.getUnitPrice()));
//					}else if(Float.valueOf("0.12").equals(orderedItemVo.getItem().getItemCategory().getVat())){
//						recette.setTva12(recette.getTva12() + (orderedItemVo.getCount() * orderedItemVo.getUnitPrice()));
//					}
//				}else if(OrderedItemType.HOSTED.equals(orderedItemVo.getType())){
//					recette.setOffert(recette.getOffert() + (orderedItemVo.getCount() * orderedItemVo.getUnitPrice()));
//				}
//				if(recette.getDiscountRate() > 0){
//					recette.setDiscount(Float.valueOf(df.format(recette.getFinalTotal() * ((float)recette.getDiscountRate()/100))));
//					recette.setTva21(Float.valueOf(df.format(recette.getTva21() * (1 - (float)recette.getDiscountRate()/100))));
//					recette.setTva12(Float.valueOf(df.format(recette.getTva12() * (1 - (float)recette.getDiscountRate()/100))));
//				}
//			}
//		}else{
//			recette.setTva6(recette.getFinalTotal());
//		}
		recette.setFinalTotal(Float.valueOf(df.format(operatedOrderVo.getOrdercat().getTotal() * (1 - (float)recette.getDiscountRate()/100))));
		this.recetteDAO.save(recette);
	}

	@Override
	public void reopenOrder(OperatedOrderVo operatedOrderVo) {
		
		Ordercat ordercat = this.ordercatDAO.findById(operatedOrderVo.getOrdercat().getId());
		ordercat.setStatus(OrderStatus.SERVICING);
		this.ordercatDAO.save(ordercat);
		
		if(operatedOrderVo.getDiningTable() != null){
			DiningTable diningTable = this.diningTableDAO.findById(operatedOrderVo.getDiningTable().getId());
			diningTable.setStatus(DiningTableStatus.OCCUPIED);
			this.diningTableDAO.save(diningTable);
		}else if(operatedOrderVo.getTakeaway() != null) {
			Takeaway takeaway = this.takeawayDAO.findById(operatedOrderVo.getTakeaway().getId());
			takeaway.setStatus(TakeawayStatus.OCCUPIED);
			this.takeawayDAO.save(takeaway);
		}
		operatedOrderVo.setOrdercat(ordercat);
	}

	@Override
	public List<ItemCategory> loadItemCategoriesByDisplayZone(int displayZone) {
		return this.itemCategoryDAO.selectByDisplayZone(displayZone);
	}

	@Override
	public boolean validateEncase(CheckoutVo checkoutVo) {
		DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
		otherSymbols.setDecimalSeparator('.');
		otherSymbols.setGroupingSeparator(','); 
		DecimalFormat df =new DecimalFormat("#.00", otherSymbols);
		
		return checkoutVo == null ? false : Float.valueOf(df.format(checkoutVo.getCard() + checkoutVo.getCash() + checkoutVo.getCheque())).equals(checkoutVo.getTotalToPay());
	}

	@Override
	public String dealCommand(OperatedOrderVo operatedOrderVo, String commandValue) {
		if(commandValue.lastIndexOf("+") == commandValue.length()-1){
			String itemLabel = commandValue.replaceAll("\\+", "");
			Item item = this.itemDAO.findByLabel(itemLabel);
			if(item == null){
				return "";
			}else{
				this.orderItem(operatedOrderVo, item.getId());
				return itemLabel;
			}
		}else if(commandValue.contains("+")){
			commandValue = commandValue.substring(commandValue.indexOf("+"));
		}
		return commandValue;
	}

	@Override
	public CheckoutVo loadCheckoutVo(int ordercatId) {
		return this.recetteDAO.findCheckouVoByOrdercatId(ordercatId);
	}


}
